

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.feature.binning.bucket_binning &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.feature.binning.bucket_binning</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.feature.binning.bucket_binning</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>

<span class="c1">#</span>
<span class="c1">#  Copyright 2019 The FATE Authors. All Rights Reserved.</span>
<span class="c1">#</span>
<span class="c1">#  Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1">#  you may not use this file except in compliance with the License.</span>
<span class="c1">#  You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1">#  Unless required by applicable law or agreed to in writing, software</span>
<span class="c1">#  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1">#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1">#  See the License for the specific language governing permissions and</span>
<span class="c1">#  limitations under the License.</span>

<span class="kn">from</span> <span class="nn">federatedml.feature.binning.base_binning</span> <span class="k">import</span> <span class="n">Binning</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.statics</span> <span class="k">import</span> <span class="n">MultivariateStatisticalSummary</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic</span> <span class="k">import</span> <span class="n">data_overview</span>


<div class="viewcode-block" id="BucketBinning"><a class="viewcode-back" href="../../../../federatedml.feature.binning.html#federatedml.feature.binning.bucket_binning.BucketBinning">[docs]</a><span class="k">class</span> <span class="nc">BucketBinning</span><span class="p">(</span><span class="n">Binning</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    For bucket binning, the length of each bin is the same which is:</span>
<span class="sd">    L = [max(x) - min(x)] / n</span>

<span class="sd">    The split points are min(x) + L * k</span>
<span class="sd">    where k is the index of a bin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">party_name</span><span class="o">=</span><span class="s1">&#39;Base&#39;</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BucketBinning</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="n">party_name</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="p">)</span>

<div class="viewcode-block" id="BucketBinning.fit_split_points"><a class="viewcode-back" href="../../../../federatedml.feature.binning.html#federatedml.feature.binning.bucket_binning.BucketBinning.fit_split_points">[docs]</a>    <span class="k">def</span> <span class="nf">fit_split_points</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Apply the binning method</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instances : DTable</span>
<span class="sd">            The input data</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        split_points : dict.</span>
<span class="sd">            Each value represent for the split points for a feature. The element in each row represent for</span>
<span class="sd">            the corresponding split point.</span>
<span class="sd">            e.g.</span>
<span class="sd">            split_points = {&#39;x1&#39;: [0.1, 0.2, 0.3, 0.4 ...],    # The first feature</span>
<span class="sd">                            &#39;x2&#39;: [1, 2, 3, 4, ...],           # The second feature</span>
<span class="sd">                            ...]                         # Other features</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">is_sparse</span> <span class="o">=</span> <span class="n">data_overview</span><span class="o">.</span><span class="n">is_sparse_data</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">is_sparse</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Bucket Binning method has not supported sparse data yet.&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>

        <span class="n">statistics</span> <span class="o">=</span> <span class="n">MultivariateStatisticalSummary</span><span class="p">(</span><span class="n">data_instances</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span><span class="p">)</span>
        <span class="n">max_dict</span> <span class="o">=</span> <span class="n">statistics</span><span class="o">.</span><span class="n">get_max</span><span class="p">()</span>
        <span class="n">min_dict</span> <span class="o">=</span> <span class="n">statistics</span><span class="o">.</span><span class="n">get_min</span><span class="p">()</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">data_instances</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
        <span class="n">final_split_points</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">max_value</span> <span class="ow">in</span> <span class="n">max_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">min_value</span> <span class="o">=</span> <span class="n">min_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
            <span class="n">split_point</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">L</span> <span class="o">=</span> <span class="p">(</span><span class="n">max_value</span> <span class="o">-</span> <span class="n">min_value</span><span class="p">)</span> <span class="o">/</span> <span class="n">n</span>
            <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bin_num</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">s_p</span> <span class="o">=</span> <span class="n">min_value</span> <span class="o">+</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">L</span>
                <span class="n">split_point</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s_p</span><span class="p">)</span>
            <span class="n">final_split_points</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">split_point</span>

        <span class="k">return</span> <span class="n">final_split_points</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>